import MSObject
import heartbeating
import time

hb = heartbeating.HeartbeatListener()
hb.start()

MSLogger.logInit('C:\\safe\\ms.safeinside.log', 2, True)

reader = WorkerMSDB.Worker('localhost', '2929', 'PGSMData', 'sa', 'safeinside')
reader.connect()
writer = WorkerPCNFirebird.Worker('192.168.0.120:C:\\pcn6\\data\\DATA.GDB', 'SYSDBA', 'masterkey')
writer.connect()

objinfo = MSObject.readObjectsFromMSDB(reader.connection.cursor(), [])
objdata = MSObject.initObjects(objinfo, {}, 'C:\\safe\\safeinside_db\\')

MSLogger.logInfo('ms safeinside driver started')

try:
    ctr = 0
    while True:
        if ctr == 6:
            objinfo = MSObject.readObjectsFromMSDB(reader.connection.cursor(), [])
            objdata = MSObject.initObjects(objinfo, {}, 'C:\\safe\\safeinside_db\\')
        
        reader.loadEvents(objdata)    #load all the today events
        MSObject.saveObjects(objdata)
        writer.saveEvents(objdata)    #save into writer's db
        MSObject.saveObjects(objdata)
        
        ctr += 1
        time.sleep(10)
except KeyboardInterrupt:
    MSLogger.logError('ms safeinside driver stopped')
    heartbeat.stop()
except Exception, err:
    MSLogger.logError('ms driver fatal error: %s' % (str(err)))
    heartbeat.stop()

